import router from './router';
// import { getToken } from 'src/util/auth';  //从cookie中获取token 
import { setStore, getStore } from 'src/config/mUtils'  // 从local Storage中获取token

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {  // 判断该路由是否需要登录权限//非登录接口都需要验证
    var token = JSON.parse(getStore('userInfo')) && JSON.parse(getStore('userInfo')).token;
    if (token !== '') {  // 判断当前的token是否存在
      next();
    } else {
      let hash = window.location.hash && window.location.hash.split('#')[1];
      hash = hash.split('=')[1] || hash;
            // 未登录就跳转到登录页面
      next({
        path: '/login',
        query: { redirect: to.fullPath }  // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })

      // 未登录就跳转到企业主页
      // window.location.href = 'http://192.168.0.136:8000/static/portal/index.htm';
    }
  }
  else {
    next();
  }
});

router.afterEach(() => {
  // NProgress.done() // finish progress bar
})
